package GUI;

import java.sql.*;

public class DBUtil {
    public Connection getConnection(){
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root","20021030");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    public void close(Connection con, Statement statement, ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(Connection con,Statement statement){
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 根据用户名和密码查询用户对象
     * @param username
     * @param pwd
     * @return
     */
    public User getByUsernameAndPwd(String username,String pwd){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from t_user where u_name=? and u_password=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,username);
            statement.setObject(2,pwd);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPwd(rs.getString("u_password"));
                user.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public User getById(int id){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from t_user where u_id=?" ;
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,id);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPwd(rs.getString("u_password"));
                user.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据用户名查询用户对象
     * @param username
     * @return
     */
    public User getByUsername(String username){
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from t_user where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,username);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_name"));
                user.setPwd(rs.getString("u_password"));
                user.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据User对象中的数据进行修改
     * @param user
     */
    public void update(User user){
        // 1. 创建连接
        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "update t_user set u_password=?,u_balance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1,user.getPwd());
            statement.setObject(2,user.getBalance());
            statement.setObject(3,user.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con,statement);
        }
    }
    public void trans(User loginuser,User otheruser){
        Connection con=getConnection();
        PreparedStatement statement=null;
        String sql="update t_user set u_password=?,u_balance=? where u_id=?";
        try {
            con.setAutoCommit(false);
            statement=con.prepareStatement(sql);
            statement.setObject(1,loginuser.getPwd());
            statement.setObject(2,loginuser.getBalance());
            statement.setObject(3,loginuser.getId());
            statement.executeUpdate();

            statement=con.prepareStatement(sql);
            statement.setObject(1,otheruser.getPwd());
            statement.setObject(2,otheruser.getBalance());
            statement.setObject(3,otheruser.getId());
            statement.executeUpdate();
            con.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(con,statement);
        }
    }
}
